/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.ecommerce.entity.impl;

import br.com.ecommerce.entity.IEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;


/**
 *
 * @author rpateixeira
 */
@Entity
@Table(name="USUARIOS")
@NamedQueries({
    @NamedQuery(name="Usuario.findInOrderByName",query="SELECT us FROM Usuario us where us.name=:name order by us.name")
})
public class Usuario  implements IEntity,Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @SequenceGenerator(name = "USUARIO_GENERATOR", sequenceName = "public.usuarioSequence", initialValue = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="USUARIO_GENERATOR")
    @Column(name="SEQ_USUARIO")
    private Long id;
    @NotEmpty(message="Nome de usuario vazio!")
    @Column(name="NOME_USUARIO",length=155,nullable=false)
    private String username;
    @NotNull
    @NotEmpty(message="Senha vazia!!")
    @Column(length=32,nullable=false)
    private String password;
    @Column(name="NOME",length=155,nullable=false)
    private String name;
    @NotEmpty
    @NotNull
    @Email
    @Column(length=155,nullable=false)
    private String email;
     @OneToMany(mappedBy="usuario",cascade=CascadeType.ALL,orphanRemoval=true)
    private List<Endereco> enderecos;
       @OneToMany(mappedBy="donoCartao",cascade=CascadeType.ALL,orphanRemoval=true)
    private List<CreditCard> creditsCards;
    @OneToMany(mappedBy="usuario",cascade=CascadeType.ALL,orphanRemoval=true)
    private List<Venda> vendas;
    
    @Override
    public Long getId() {
        return this.id;
    }

    
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
   
    public List<Endereco> getEnderecos() {
        if(enderecos==null){
            enderecos= new ArrayList<Endereco>();
        }
        return enderecos;
    }

    public void setEnderecos(List<Endereco> enderecos) {
        this.enderecos = enderecos;
    }
  
    public List<CreditCard> getCreditsCards() {
        if(creditsCards==null){
            creditsCards= new LinkedList<CreditCard>();
        }
        return creditsCards;
    }

    public void setCreditsCards(List<CreditCard> creditsCards) {
        this.creditsCards = creditsCards;
    }
    public List<Venda> getVendas() {
        if(vendas==null){
            vendas= new LinkedList<Venda>();
        }
        return vendas;
    }

    public void setVendas(List<Venda> vendas) {
        this.vendas = vendas;
    }
    
    public void addEndereco(Endereco endereco){
        endereco.setUsuario(this);
        getEnderecos().add(endereco);
    }

    public void addCreditCard(CreditCard cc){
        cc.setDonoCartao(this);
        getCreditsCards().add(cc);
    }

    public void addVenda(Venda v){
        v.setUsuario(this);
        getVendas().add(v);
    }
}
